package com.api.user.service.impl;

import com.api.core.ServiceException;
import com.api.user.dao.DisplayAreaMapper;
import com.api.user.dao.GameProfileMapper;
import com.api.user.model.DisplayArea;
import com.api.user.model.GameProfile;
import com.api.user.service.GameProfileService;
import com.api.core.service.AbstractService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.api.common.JSONUtils;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * Created by wanghuiwen on 2021/03/23.
 */
@Service
@Transactional
public class GameProfileServiceImpl extends AbstractService<GameProfile> implements GameProfileService {
    @Resource
    private GameProfileMapper gameProfileMapper;
    @Resource
    private DisplayAreaMapper displayAreaMapper;

    @Override
    public Result list(String search, String order, Integer page, Integer size){
        Map<String, Object> params = JSONUtils.json2map(search);
        Map<String, Object> orderParams = JSONUtils.json2map(order);
        for (String key : orderParams.keySet()) {
                if (orderParams.get(key) != null && orderParams.get(key).equals("ascending")) orderParams.put(key, "asc");
                if (orderParams.get(key) != null && orderParams.get(key).equals("descending")) orderParams.put(key, "desc");
            }
        PageHelper.startPage(page, size);
        List<Map<String, Object>> res = gameProfileMapper.baseList(params, orderParams);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(res);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @Override
    public Result listByUser(String search, String order, Integer page, Integer size, Long userId) {
        Map<String, Object> params = JSONUtils.json2map(search);
        params.put("userId",userId);
        Map<String, Object> orderParams = JSONUtils.json2map(order);
        List<Map<String, Object>> res = gameProfileMapper.list(params, orderParams);
        for (int i = 0; i < res.size(); i++) {
           List<DisplayArea> displayAreas = displayAreaMapper.getByCompanionGameId((Long) res.get(i).get("companionGameId"));
           res.get(i).put("displayAreas",displayAreas);
        }
        return ResultGenerator.genSuccessResult(res);
    }

    @Override
    public Result add(GameProfile gameProfile) {
        if(gameProfile.getProfileId()!=null){
            GameProfile oldProfile =  gameProfileMapper.selectByPrimaryKey(gameProfile.getProfileId());
            if(!oldProfile.getGameId().equals(gameProfile.getGameId()) || !oldProfile.getIgn().equals(gameProfile.getIgn()) || !oldProfile.getIgid().equals(gameProfile.getIgid())){
                checkSameGameprofile(gameProfile);
            }
            this.update(gameProfile);
        }else{
            checkSameGameprofile(gameProfile);
            this.save(gameProfile);
        }
        return ResultGenerator.genSuccessResult(gameProfile);
    }

    private void checkSameGameprofile(GameProfile gameProfile) {
        GameProfile profile = gameProfileMapper.findByUserAndGame(gameProfile.getGameId(),gameProfile.getUserId());
        if(profile!=null) throw  new ServiceException("The same game can only be added once");
        int count =  gameProfileMapper.findByGameAndGmId(gameProfile.getGameId(),gameProfile.getIgid(),gameProfile.getIgn());
        if(count>0) throw  new ServiceException("The same game can not add same igid or ign");
    }

    @Override
    public List<GameProfile> findByUserId(Long id) {
        return gameProfileMapper.findByUserId(id);
    }
}
